package com.qcit.older.usercenter.common

import android.util.Log
import com.qcit.common.utils.QSharedPreferences
import okhttp3.Interceptor
import okhttp3.Response
import java.text.SimpleDateFormat
import java.util.*
import java.util.logging.Logger

class QRequesterLogInterceptor : Interceptor {

    val tag = "QCIT_Retrofit"
    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        var builder = request.newBuilder()
        QSharedPreferences.user?.let {
            var auth = it.authorization
            if (auth == null) auth = ""
            builder.addHeader("Authorization", auth);
        }
        Log.i(
            tag,
            format.format(Date()) + " Requeste " + "\nmethod:" + request.method + "\nurl:" + request.url + "\nbody:" + request.body
        )
        var response = chain.proceed(request)
        Log.i(
            tag,
            format.format(Date()) + " Response " + "\nsuccessful:" + response.isSuccessful + "\nbody:" + response.peekBody(
                1024
            )?.string()
        )
        return response
    }

}